Introduction

The KeyTech Windows Service Manager ActiveX control 1.0 simplifies access to the Windows NT Service Control Manager (SCM) for the Visual Basic programmer. It models the SCM using three object classes: ServiceManager; ExtendedServiceStatus; and ServiceConfiguration.

ServiceManager represents the SCM. ExtendedServiceStatus represents the extended status information for a particular service. ServiceConfiguration represents the configuration for a particular service.

So, for example, the following code demonstrates simple manipulation of a service:

Dim ServiceManager As New ServiceManager

With ServiceManager
    
' Set access to allow starting service
    .AccessMode = WriteAccess

    
' Start my service if it's stopped
    If .Status("MyService") = Stopped Then
        .Start "MyService"
    
End If
End
With

The control encapsulates the most commonly used features of the native Windows NT SCM API including:

The control is intended for use with Visual Basic 5.0 or later releases running under Windows NT 4.0. It may also be used from within VBScript or JScript running under Internet Explorer 4.0 or later releases. Windows 95/98 users may access the control running on a Windows NT computer using DCOM.

Support

For further information, visit us at http://www.keytech.com.au or send email to info@keytech.com.au.

To report bugs or suggest enhancements for future releases send email to support@keytech.com.au.

This document assumes the reader has an understanding of the Windows NT SCM and the associated Win32 API. For further details refer to the relevant Microsoft documentation.

Installation

For users of previous versions of the Windows NT Service Manager ActiveX control ensure that it is uninstalled before installing the new version.

Copy the Service Manager DLL to the system directory and register it by running regsvr32 KtSrvMgr.dll. Compile and run the sample Visual Basic programs making sure to set the project references to include the Service Manager control.

To uninstall, run regsvr32 /u KtSrvMgr.dll and remove the DLL.

ServiceManager Class

The ServiceManager class allows access to the Service Control Manager (SCM) and manipulation of Windows services.

To use the ServiceManager create an object using the new operator or the CreateObject function. Set the ComputerName if a remote computer is being accessed. Set the AccessMode to WriteAccess if a write operation is to be performed. Perform the required operations on the named service.

The service name is the unique name of the service as defined within the Windows registry. The display name is the name displayed, for example, by the Services control applet. Use DisplayName to convert a service name to a display name. Use ServiceName to convert a display name to a service name.

The sample code illustrates the use of this class. For a more detailed example refer to the example code in KtSrvMgrEx1.

Property ComputerName As String
Property AccessMode As AccessMode
Sub LockDatabase()
Sub UnlockDatabase()
Property DatabaseLocked As Boolean ' Read only
Sub Create(ServiceName As String, DisplayName As String, FileName As String)
Sub Delete(ServiceName As String)
Sub Start(ServiceName As String, Optional Arguments As Variant)
Sub Stop(ServiceName As String)
Sub Pause(ServiceName As String)
Sub Continue(ServiceName As String)
Sub Control(ServiceName As String, ControlCode As Long)
Property Status(ServiceName As String) As ServiceStatus ' Read only
Property ExtendedStatus(ServiceName As String) As ExtendedServiceStatus ' Read only
Property DisplayName(ServiceName As String) As String ' Read only
Property ServiceName(DisplayName As String) As String ' Read only 
Property Configuration(ServiceName As String) As ServiceConfiguration ' Read only
Property Dependencies(ServiceName As String) As Variant ' Read only
Property Services(ServiceName As String) As Variant ' Read only

ComputerName

The ComputerName identifies the computer whose Service Control Manager (SCM) is to be accessed. If no name is specified then the local computer is assumed.

Dim ServiceManager As New ServiceManager

ServiceManager.ComputerName = "MyServer"

AccessMode

The AccessMode specifies the type of access required. For queries ReadAccess is sufficient. To change the state of a service or modify the configuration WriteAccess is required. If the AccessMode isn't set it defaults to ReadAccess.

Dim ServiceManager As New ServiceManager

ServiceManager.AccessMode = WriteAccess

LockDatabase

Lock the database for exclusive use prior to changing the configuration. Although not essential, it is recommended that the services database is locked prior to any configuration changes and unlocked as soon as possible. Services are configured using the ServiceConfiguration.

Dim ServiceManager As New ServiceManager

ServiceManager.AccessMode = WriteAccess
ServiceManager.LockDatabase
    
' Make any configuration changes
ServiceManager.UnlockDatabase

UnlockDatabase

Unlock the database following a previous lock.

Dim ServiceManager As New ServiceManager

ServiceManager.AccessMode = WriteAccess
ServiceManager.LockDatabase
    
' Make any configuration changes
ServiceManager.UnlockDatabase

DatabaseLocked

Indicate whether the database is locked by the caller or some other process.

Dim ServiceManager As New ServiceManager

If ServiceManager.DatabaseLocked Then
    
' Handle locked database
End If

Create

Create a service in the services database contained within the Windows registry. Once created the service may be configured using the ServiceConfiguration. The service name and display name must be supplied as well as the path to the executable for the service.

Dim ServiceManager As New ServiceManager

ServiceManager.AccessMode = WriteAccess
ServiceManager.Create "MyService", "This is my service", "c:\myservice\myservice.exe"

Delete

Delete a service from the services database contained within the Windows registry. The service is identified by its name.

Dim ServiceManager As New ServiceManager

ServiceManager.AccessMode = WriteAccess
ServiceManager.Delete "MyService"

Start

Start the named service.

Dim ServiceManager As New ServiceManager

ServiceManager.AccessMode = WriteAccess
ServiceManager.Start "MyService"

Stop

Stop the named service.

Dim ServiceManager As New ServiceManager

ServiceManager.AccessMode = WriteAccess
ServiceManager.Stop "MyService"

Pause

Pause the named service.

Dim ServiceManager As New ServiceManager

ServiceManager.AccessMode = WriteAccess
ServiceManager.Pause "MyService"

Continue

Pause the named service.

Dim ServiceManager As New ServiceManager

ServiceManager.AccessMode = WriteAccess
ServiceManager.Continue "MyService"

Control

Send a control code to the named service. The control code must be within the range of 128 to 255.

Dim ServiceManager As New ServiceManager

ServiceManager.AccessMode = WriteAccess
ServiceManager.Control "MyService", 128

Status

Get the current status of the named service. The returned status is of type ServiceStatus.

Dim ServiceManager As New ServiceManager

Select Case ServiceManager.Status("MyService")
    
case Stopped
        
' Service is stopped

    case StartPending
        
' Service is starting

    case StopPending
        
' Service is stopping

    case Running
        
' Service is running

    case ContinuePending
        
' Service is about to continue

    case PausePending
        
' Service is pausing

    case Paused
        
' Service is paused
End Select

ExtendedStatus

Get the current extended status, ExtendedServiceStatus, of the named service.

Dim ServiceManager As New ServiceManager
Dim ExtendedServiceStatus As ExtendedServiceStatus

Set ExtendedServiceStatus = ServiceManager.ExtendedStatus("MyService")

With ExtendedServiceStatus
    
' Access the extended status
End With

DisplayName

Get the display name given the service name. The service name is the unique name of the service as defined within the Windows registry. The display name is the name displayed, for example, by the Services control applet. To set the display name, use the ServiceConfiguration class.

Dim ServiceManager As New ServiceManager
Dim DisplayName As String

DisplayName = ServiceManager.DisplayName("MyService")

ServiceName

Get the service name given the display name.

Dim ServiceManager As New ServiceManager
Dim ServiceName As String

ServiceName = ServiceManager.ServiceName("This is my service")

Configuration

Get the configuration information of the named service. The returned ServiceConfiguration object may be used to get and set configuration data.

Dim ServiceManager As New ServiceManager
Dim ServiceConfiguration As ServiceConfiguration

Set ServiceConfiguration = ServiceManager.Configuration("MyService")

With ServiceConfiguration
    
' Access the configuration
End With

Dependencies

Get the array of dependent service names for the named service. If there are no dependencies the returned variant is empty. Otherwise it contains an array of strings. To set the dependencies, use the ServiceConfiguration class.

Dim ServiceManager As New ServiceManager
Dim v As Variant

v = ServiceManager.Dependencies("MyService")

Services

Get the array of all service names. If there are no services (extremely unlikely!) the returned variant is empty. Otherwise it contains an array of strings.

Dim ServiceManager As New ServiceManager
Dim v As Variant

v = ServiceManager.Services

ExtendedServiceStatus Class

The ExtendedServiceStatus class allows access to the extended status for a service.

To access the ExtendedServiceStatus retrieve an object reference using the ExtendedStatus property. Use the various properties to get the extended status.

Property Status As ServiceStatus ' Read only
Property ControlsAccepted As ControlsAccepted ' Read only
Property CheckPoint As Long ' Read only
Property WaitHint As Long ' Read only
Property Win32ExitCode As Long ' Read only
Property SpecificExitCode As Long ' Read only

Status

Get the current status of the service. The returned status is of type ServiceStatus.

Dim ServiceManager As New ServiceManager
Dim ExtendedServiceStatus As ExtendedServiceStatus

Set ExtendedServiceStatus = ServiceManager.ExtendedStatus("MyService")

Select Case ExtendedServiceStatus.Status
    
case Stopped
        
' Service is stopped

    case StartPending
        
' Service is starting

    case StopPending
        
' Service is stopping

    case Running
        
' Service is running

    case ContinuePending
        
' Service is about to continue

    case PausePending
        
' Service is pausing

    case Paused
        
' Service is paused
End Select

ControlsAccepted

Get the controls accepted by the service. The returned controls accepted is one or more of type ControlsAccepted.

Dim ServiceManager As New ServiceManager
Dim ExtendedServiceStatus As ExtendedServiceStatus

Set ExtendedServiceStatus = ServiceManager.ExtendedStatus("MyService")

If ExtendedServiceStatus.ControlsAccepted And AcceptPauseContinue Then
    
' Service accepts pause and continue
End If

CheckPoint

Get the checkpoint value returned by the service.

Dim ServiceManager As New ServiceManager
Dim ExtendedServiceStatus As ExtendedServiceStatus
Dim CheckPoint As Long

Set
ExtendedServiceStatus = ServiceManager.ExtendedStatus("MyService")
CheckPoint = ExtendedServiceStatus.CheckPoint

WaitHint

Get the wait hint value returned by the service.

Dim ServiceManager As New ServiceManager
Dim ExtendedServiceStatus As ExtendedServiceStatus
Dim WaitHint As Long

Set
ExtendedServiceStatus = ServiceManager.ExtendedStatus("MyService")
WaitHint = ExtendedServiceStatus.WaitHint

Win32ExitCode

Get the Win32 exit code returned by the service.

Dim ServiceManager As New ServiceManager
Dim ExtendedServiceStatus As ExtendedServiceStatus
Dim ExitCode As Long

Set
ExtendedServiceStatus = ServiceManager.ExtendedStatus("MyService")
ExitCode = ExtendedServiceStatus.Win32ExitCode

SpecificExitCode

Get the service specific exit code returned by the service.

Dim ServiceManager As New ServiceManager
Dim ExtendedServiceStatus As ExtendedServiceStatus
Dim ExitCode As Long

Set
ExtendedServiceStatus = ServiceManager.ExtendedStatus("MyService")
ExitCode = ExtendedServiceStatus.SpecificExitCode

ServiceConfiguration Class

The ServiceConfiguration class allows access to the configuration information for a service.

To use the ServiceConfiguration retrieve an object reference using the Configuration property. Use the various properties to get and set the configuration. You must have write access to update the configuration.

KtSrvMgrEx1 demonstrates getting and setting configuration information.

Property ServiceType As ServiceType
Property StartType As StartType
Property ErrorControl As ErrorControl
Property FileName As String
Property LoadOrderGroup As String
Property TagId As Long
Property Dependencies As Variant
Property AccountName As String
Property Password As String ' Write only
Property DisplayName As String

ServiceType

Get or set the service type. For Windows services, as opposed to device drivers, the service type can be either Win32OwnProcess or Win32ShareProcess and optionally InteractiveProcess.

Dim ServiceManager As New ServiceManager
Dim ServiceConfiguration As ServiceConfiguration

Set ServiceConfiguration = ServiceManager.Configuration("MyService")
ServiceConfiguration.ServiceType = Win32OwnProcess

StartType

Get or set the service start type.

Dim ServiceManager As New ServiceManager
Dim ServiceConfiguration As ServiceConfiguration

Set ServiceConfiguration = ServiceManager.Configuration("MyService")
ServiceConfiguration.StartType = DemandStart

ErrorControl

Get or set the service error control.

Dim ServiceManager As New ServiceManager
Dim ServiceConfiguration As ServiceConfiguration

Set ServiceConfiguration = ServiceManager.Configuration("MyService")
ServiceConfiguration.ErrorControl = Normal

FileName

Get or set the file name of the service's executable.

Dim ServiceManager As New ServiceManager
Dim ServiceConfiguration As ServiceConfiguration

Set ServiceConfiguration = ServiceManager.Configuration("MyService")
ServiceConfiguration.FileName = "c:\myservice\myservice.exe"

LoadOrderGroup

Get or set the name of the service's load order group. An empty string specifies no load order group.

Dim ServiceManager As New ServiceManager
Dim ServiceConfiguration As ServiceConfiguration

Set ServiceConfiguration = ServiceManager.Configuration("MyService")
ServiceConfiguration.LoadOrderGroup = ""

TagId

Get or set the service's tag identifier. The tag identifier specifes the load order within a group and is only evaluated for device driver services.

Dim ServiceManager As New ServiceManager
Dim ServiceConfiguration As ServiceConfiguration
Dim TagId As Long

Set
ServiceConfiguration = ServiceManager.Configuration("MyService")
TagId = ServiceConfiguration.TagId

Dependencies

Get or set the service's dependencies. Dependencies are identified by service name. If there are no dependencies the returned variant is empty. Otherwise it contains an array of strings.

Dim ServiceManager As New ServiceManager
Dim ServiceConfiguration As ServiceConfiguration

Set ServiceConfiguration = ServiceManager.Configuration("MyService")
ServiceConfiguration.Dependencies =
Array("OtherService1", "OtherService2")

AccountName

Get or set the name of the account under which the service runs. The account name can be either LocalSystem to specify the local system account or be in the form UserName for a local account or DomainName\Username for a domain account. If the service type is Win32ShareProcess the local system account must be used.

Dim ServiceManager As New ServiceManager
Dim ServiceConfiguration As ServiceConfiguration

Set ServiceConfiguration = ServiceManager.Configuration("MyService")
ServiceConfiguration.AccountName = "LocalSystem"

Password

Set the password for the account under which the service runs.

Dim ServiceManager As New ServiceManager
Dim ServiceConfiguration As ServiceConfiguration

Set ServiceConfiguration = ServiceManager.Configuration("MyService")
ServiceConfiguration.Password = "Top Secret"

DisplayName

Get or set the service's display name. The display name may be the same as the service's name but cannot be the same as any other service's name or display name.

Dim ServiceManager As New ServiceManager
Dim ServiceConfiguration As ServiceConfiguration

Set ServiceConfiguration = ServiceManager.Configuration("MyService")
ServiceConfiguration.DisplayName = "This is my service"

Sample Code

Sample code, developed under Visual Basic 6.0,  is included with the control. If you wish to build this code, don't forget to include the control using the Project | References dialog.

KtSrvMgrEx1 demonstrates most SCM operations.

KtSrvMgrEx2 demonstrates using the Service Manager control from within an HTML page. These examples require Internet Explorer 4.0 or a later release.

DCOM Configuration

Under most circumstances the Service Manager control will be used as an inproc server as access to remote SCMs may be achieved using the ComputerName property. However the control can be accessed as a remote out-of-proc server using a DLL surrogate (eg. DllHost.exe).

The Microsoft DCOM configuration utility, dcomcnfg.exe,  must be run on the remote server to set the access, launch, and configuration permissions as well as the user account. If these aren't set correctly a permission error will occur.

Introductory information on configuring DCOM may be found at our web site. For further details refer to the relevant Microsoft documentation.